Hello and welcome back to our cost.
So in this session we are creating random numbers with the unpaid package.
And we have already learned that we can create random random numbers with the random module of the standard
python library.
However creating random numbers for smart umpires more flexible and provides a lot more functionality.
And it's also fast so so there's actually no reason to use the random module loft space and standard
library even we can use the power for the name pi random package.
So now let's first import pi and then what we would do you want to create a 10 random integers between
1 and 101 excluding so let's press shift tab and you can see.
So random dot Rand int returns integers font lo inclusive to high exclusive.
So if you want random integers between 1 and 100 including we have to type year 101 into the third parameter
is the size.
So how many random numbers or integers we want to create let's execute the of here.
Now you can see we get 10 random integers in the range one to one hundred and if we repeat this process
we should get a different 10 random in the just because it's random and that's a very helpful instrument
called random seed.
So random that seed creates so-called absurd or random numbers and it enables us to reproduce the other
random numbers we create.
So whenever you use them the same seed as ISV in brackets if you have to define a number so here it's
1 2 3 but can be any number and whenever you have we had the same number and the random seed to them
you create exactly the same random numbers as I do.
So let's run near the center and that's repeated the process and we should get the same numbers again
because we didn't change for the random seed.
All right.
And we can also create normally distributed numbers.
I don't want to go into details of normal distribution but it's kind of a better shape probability distribution
where we can determine the means or minus 5.
But it's flexible and you can determine the standard deviation.
So the second position is 2.
So Locke stands for the mean scale stands for the standard deviation and you can also determine the
size.
So how many normal distribution numbers you want to get.
So when we executed we had 10 normal distribution numbers with mean 5 and standard deviation 2.
All right so let's create an array with numbers between 1 and 100 so called B and then we can randomly
shuffle all elements here so we can mix mix all elements up and change the sorting.
Now we have to print B and you can see so be it.
Now we are randomly sorted are not sorted.
Actually but that's the method topside and where we can start again our unsorted array and let's check
the results.
Now it's sorted again now and actually for whatever reason so the method sort does not provide us the
option to to sort the array in an inverse order or our reverse order.
So I don't know why there's no option for it but yeah it's the case but we if you want to sort our array
in the reverse order there's a trick here by double colon minus one where we have the reverse order
starting from hundreds till 1 and that's another method called random that's why it's so random that
choice creates a random sample from a given array so we can have our array B and then we determine the
size of our sample.
So for example one hundred and then we can determine whether the sample is with or without replacement.
So what does that mean.
Whenever an element is randomly chosen so let's say 1 the next step.
Again we have the chance to choose this element.
And with out replacement means that you can choose one element only once.
So here we have a replacement is true and we choose a randomly select the sample of 100 elements from
our array B.
Let's see what we get.
OK.
We get a hundred elements and we can then swap our new sample array B1 and they can see so we have some
duplicates so we have two was chosen two times four was chosen four times and with the method unique
we can choose only the unique elements that we can remove all duplicates.
There you can see we have only the unique values without any duplicates and you would get the same result
by transforming our array into a set and then transforming it into a list and then to our right.
So we cannot immediately transform a set into an array.
So first we have to make a list and we get the same result here.
And now by adding the method types you can determine how many unique elements we have here.
You determine the unique elements of our sample and then that size gives us them the amount of unique
values or elements of it 66.
So we created 100 integers with replacement and we got back 66 unique elements and the unique method
is quite informative.
So it has some more parameters for example a return index it goes through and return count sequence
true.
So by default they are set to false.
Let's see what we get.
So here in the first array we have our unique elements and in the second array we get the index position
of the first occurrence of our unique values.
So for example if we go to our array with duplicates.
The first occurrences to us of course at position 0 and the first occurrence of 3 is that position in
x position to that I can see here and also kind of returns as an array with return counts.
We can read that to us included in our sample two times three was included in our sample one time and
four was then included in our sample four times.
So this is quite informative here and we can also set the replace parameter to forwards here
now there is actually no surprise because our array contains 100 elements and the free randomly select
100 elements without replacement.
How we actually get all 100 elements so check unique elements so we get all elements from 1 to 100
and we have 100 unique elements.
All right.
So this was a short introduction to the number random package and we learned to create random integers
normal distributed numbers and how to randomly shuffle an array.
And also we learned how to make a random sample of an array and there are many more things you can do
with the random module.
So for example that's NPR random thought.
And then if you press tab there you can see how many methods.
You can.
You can apply here so you can have an anomalous simulation.
And yes many many things you can ask the price and distribution and so on and so on.
All right.
So this is it for the time being so this was a short intro to random numbers and the next session we
will compare the python standard library with the PI packets in terms of performance so hope to see
you there and by.